// 更新当前时间显示
function updateCurrentTime() {
    const now = new Date();
    const timestamp = now.getTime();
    const dateStr = now.toLocaleString();
    document.getElementById('currentTime').innerHTML = 
        `当前时间：${dateStr}<br>时间戳：${timestamp}`;
}

// 时间戳转日期
function timestampToDate() {
    const timestamp = document.getElementById('timestamp').value;
    try {
        const date = new Date(parseInt(timestamp));
        if (isNaN(date.getTime())) {
            throw new Error('无效的时间戳');
        }
        document.getElementById('dateResult').textContent = date.toLocaleString();
    } catch (error) {
        showToast('请输入有效的时间戳');
    }
}

// 日期转时间戳
function dateToTimestamp() {
    const datetime = document.getElementById('datetime').value;
    if (!datetime) {
        showToast('请选择日期时间');
        return;
    }
    const timestamp = new Date(datetime).getTime();
    document.getElementById('timestampResult').textContent = timestamp;
}

// 复制当前时间戳
function copyCurrentTimestamp() {
    const timestamp = new Date().getTime().toString();
    navigator.clipboard.writeText(timestamp).then(() => {
        showToast('时间戳已复制到剪贴板');
    });
}

// 显示提示框
function showToast(message) {
    const toast = document.getElementById('toast');
    toast.textContent = message;
    toast.classList.add('show');
    setTimeout(() => {
        toast.classList.remove('show');
    }, 2000);
}

// 页面加载时更新当前时间
updateCurrentTime();
// 每秒更新一次当前时间
setInterval(updateCurrentTime, 1000); 